import java.util.*;

public class Solution1797 {

    static class Slot{
        String a;
        int b;

        public Slot(String a, Integer b) {
            this.a = a;
            this.b = b;
        }

        public String getA() {
            return a;
        }

        public Integer getB() {
            return b;
        }
    }

    int time;
    Map<String,Integer> map=new HashMap<>();
    PriorityQueue<Slot> queue=new PriorityQueue<>(Comparator.comparingInt(Slot::getB));

    public Solution1797(int timeToLive) {
        time=timeToLive;
    }

    public void generate(String tokenId, int currentTime) {
        while (!queue.isEmpty() && queue.peek().getB()<=currentTime){
            if (queue.peek().getB().equals(map.get(queue.peek().getA()))){
                map.remove(queue.peek().getA());
            }
            queue.poll();
        }
        map.put(tokenId,currentTime+time);
        queue.offer(new Slot(tokenId, currentTime + time));
    }

    public void renew(String tokenId, int currentTime) {
        while (!queue.isEmpty() && queue.peek().getB()<=currentTime){
            if (queue.peek().getB().equals(map.get(queue.peek().getA()))){
                map.remove(queue.peek().getA());
            }
            queue.poll();
        }
        if (map.containsKey(tokenId)) {
            map.put(tokenId,currentTime+time);
            queue.offer(new Slot(tokenId, currentTime + time));
        }
    }

    public int countUnexpiredTokens(int currentTime) {
        while (!queue.isEmpty() && queue.peek().getB()<=currentTime){
            if (queue.peek().getB().equals(map.get(queue.peek().getA()))){
                map.remove(queue.peek().getA());
            }
            queue.poll();
        }
        return map.size();
    }

    public static void main(String[] args) {
        Solution1797 solution1797=new Solution1797(5);
        solution1797.renew("aaa",1);
        solution1797.generate("aaa",2);
        System.out.println(solution1797.countUnexpiredTokens(6));
        solution1797.generate("bbb",7);
        solution1797.renew("aaa",8);
        solution1797.renew("bbb",10);
        System.out.println(solution1797.countUnexpiredTokens(15));
    }
}
